package com.zhuangwei.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhuangwei.domain.MybatisUser;
import com.zhuangwei.mapper.MybatisUserMapper;
import com.zhuangwei.service.MybatisUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LeviCoder
 * @create 2019-01-29 19:35
 */
@Slf4j
@Service
public class MybatisUserServiceImpl implements MybatisUserService
{
    @Autowired
    private MybatisUserMapper mybatisUserMapper;

    @Override
    public MybatisUser save(MybatisUser user)
    {
        mybatisUserMapper.insertUseGeneratedKeys(user);
        return user;
    }

    @Override
    public List<MybatisUser> saveList(List<MybatisUser> userList)
    {
        mybatisUserMapper.insertList(userList);
        return userList;
    }

    @Override
    public MybatisUser update(MybatisUser user)
    {
        int i = mybatisUserMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            log.info("【MybatisUserService】更新成功：{}条目", i);
        } else {
            log.error("【MybatisUserService】更新失败：{}", user);
        }
        return mybatisUserMapper.selectByPrimaryKey(user.getId());
    }

    @Override
    public Integer delete(MybatisUser user)
    {
        return mybatisUserMapper.deleteByPrimaryKey(user);
    }

    @Override
    public MybatisUser findById(Long id)
    {
        MybatisUser mybatisUser = mybatisUserMapper.selectByPrimaryKey(id);
        if (mybatisUser == null ){
            try
            {
                throw new Exception("对象不存在");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return mybatisUser;
    }

    @Override
    public MybatisUser findByName(String name)
    {
        return mybatisUserMapper.findByName(name);
    }

    @Override
    public List<MybatisUser> findAll()
    {
        return mybatisUserMapper.selectAll();
    }

    @Override
    public List<MybatisUser> findMybatisUserByPage(Integer pageNum, Integer pageSize)
    {
        PageHelper.startPage(pageNum, pageSize);
        return mybatisUserMapper.selectAll();
    }
}
